﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HuynhQuangWebsite.Models;
namespace HuynhQuangWebsite.Controllers
{
    public class DashBoardController : BaseController
    {
        //
        // GET: /DashBoard/
        huynhquangco_websiteEntities ctx = new huynhquangco_websiteEntities();
        public ActionResult DashBoard()
        {
            return View();
        }
        // **************************************
        // URL: /AdminSite/LogOnDashBoard
        // **************************************
        public ActionResult LogOnDashBoard()
        {
            var adm = new Models.Admin();
            return View(adm);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult LogOnDashBoard(FormCollection collection)
        {
            try
            {
                EnCrypto encrypto = new EnCrypto();
                String userName = collection[0].ToString();
                String password = collection[1].ToString();
                if (String.IsNullOrWhiteSpace(userName))
                    return RedirectToAction("LogOnDashBoard", "DashBoard");
                //Models.Admin adm = ctx.GetAdminByName(userName).SingleOrDefault(p => p.UserName == userName);
                var adm = (from admin in ctx.Admins where admin.UserName == userName select admin).Single();
                if (adm != null)
                {
                    if (adm.Type == "Admin")
                    {
                        string a = encrypto.Decrypt(adm.Password);
                        if (adm.Password == encrypto.Encrypt(password))
                        {
                            Session["Admin"] = adm.AdminID;
                            Session["Type"] = adm.Type;
                            //adm.PCIP = GetCookie();
                            ctx.SaveChanges();
                            return RedirectToAction("DashBoard", "DashBoard");
                        }
                        else
                        {
                            Session["Admin"] = null;
                            Session["Type"] = null;
                            ViewData["Msg"] = "Mật khẩu không chính xác!";
                            return View();
                        }
                    }
                    else
                    {
                        if (adm.AllowLogOn)
                        {
                            if (adm.Password == encrypto.Encrypt(password))
                            {
                                Session["Admin"] = adm.AdminID;
                                Session["Type"] = adm.Type;
                                adm.PCIP = GetCookie();
                                ctx.SaveChanges();
                                return Redirect("Index");
                            }
                            else
                            {
                                Session["Admin"] = null;
                                Session["Type"] = null;
                                ViewData["Msg"] = "Mật khẩu không chính xác!";
                                return View();
                            }
                        }
                        else
                        {
                            ViewData["Msg"] = "Hiện tại bạn không có quyền đăng nhập hệ thống!";
                            return View();
                        }
                    }

                }
                else
                {
                    ViewData["Msg"] = "Người dùng không tồn tại!";
                    return View();
                }
                //return View();
            }
            catch (Exception ex)
            {
                ViewData["Msg"] = ex.Message;
                return View();
            }
        }
        public ActionResult Logout()
        {
            Session["Admin"] = null;
            Session["Type"] = null;
            Server.ClearError();
            return RedirectToAction("LogOn", "DashBoard");
        }
    }
}
